package cn.btks.resultintegration.rule.service.impl;

import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.rule.pojo.AdmittanceValue;
import cn.btks.resultintegration.rule.pojo.SubstanceParam;
import cn.btks.resultintegration.rule.repository.AdmittanceValueRepository;
import cn.btks.resultintegration.rule.service.AdmittanceValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2021/10/15 15:13
 */
@Service
public class AdmittanceValueServiceImpl implements AdmittanceValueService {

    @Autowired
    AdmittanceValueRepository admittanceValueRepository;

    @Override
    public AdmittanceValue findById(Long id) {
        Optional<AdmittanceValue> optional = admittanceValueRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public List<AdmittanceValue> findAll() {
        return admittanceValueRepository.findAll();
    }

    @Override
    public ResultState save(AdmittanceValue admittanceValue) {
        AdmittanceValue save = admittanceValueRepository.save(admittanceValue);
        return save != null ? new ResultState(StateInfo.SAVE_OK.getCode(), StateInfo.SAVE_OK.getMsg(), save) :
                new ResultState(StateInfo.SAVE_ERROR.getCode(), StateInfo.SAVE_ERROR.getMsg());
    }

    @Override
    public ResultState update(AdmittanceValue admittanceValue) {
        if (admittanceValue.getId() != null) {
            AdmittanceValue update = admittanceValueRepository.save(admittanceValue);
            return update != null ? new ResultState(StateInfo.SAVE_OK.getCode(), StateInfo.SAVE_OK.getMsg(), update) :
                    new ResultState(StateInfo.SAVE_ERROR.getCode(), StateInfo.SAVE_ERROR.getMsg());
        }
        return new ResultState(StateInfo.SAVE_ERROR1.getCode(), StateInfo.SAVE_ERROR1.getMsg());
    }

    @Override
    public ResultState delete(Long id) {
        Optional<AdmittanceValue> optional = admittanceValueRepository.findById(id);
        if (optional.isPresent()) {
            admittanceValueRepository.deleteById(optional.get().getId());
            return new ResultState(StateInfo.DELETE_OK.getCode(), StateInfo.DELETE_OK.getMsg());
        }
        return new ResultState(StateInfo.DELETE_ERROR_2.getCode(), StateInfo.DELETE_ERROR_2.getMsg());
    }
}
